#include<iostream>
using namespace std;
int m; //m为操作次数
typedef struct PNode {
    float coef; //系数
    int expn; //指数
    struct PNode *next;
} PNode, *Polynomial;

void AddPolyn(Polynomial &Pa, Polynomial &Pb){
    PNode *p1 = Pa->next, *p2 = Pb->next, *pre = Pa;
    while(p1 && p2){
        if(p1->expn < p2->expn){
            pre = p1;
            p1 = p1->next;
        } else if(p1->expn > p2->expn){
            PNode *t = p2;
            p2 = p2->next;
            t->next = p1;
            pre->next = t;
            pre = t;
        } else {
            p1->coef += p2->coef;  
            if(p1->coef == 0){
                pre->next = p1->next;
                delete p1;
                p1 = pre->next;
            } else {
                pre = p1;
                p1 = p1->next;
            }
            PNode *t = p2;
            p2 = p2->next;
            delete t;
        }
    }
    if(p2){
        pre->next = p2;
    }
    Pb->next = NULL;
}


void CreatPolyn(Polynomial &P, int n){
    P = new PNode;
    P->next = NULL;
    while(n--){
        PNode *a = new PNode;
        cin >> a->coef >> a->expn;
        a->next = NULL;

        PNode *pre = P;
        PNode *cur = P->next;
        while(cur && cur->expn < a->expn){
            pre = cur;
            cur = cur->next;
        }
        if(cur && cur->expn == a->expn){
            cur->coef += a->coef;
            delete a;
            if(cur->coef == 0){
                pre->next = cur->next;
                delete cur;
            }
        }else{
            a->next = cur;
            pre->next = a;
        }
    }
}


void Out_list(Polynomial L){//输出多项式的系数和指数
    Polynomial p;
    p = L->next;
    while (p){
        cout <<p->coef<<' '<<p->expn<<' ';
        p = p->next;
    }
    cout<<endl;
}

int main() {
    Polynomial Pa,Pb;
    int j,k;
    cin >> m;     //m次操作
    while(m --) {
        char op;
        cin >> op;
        switch(op){
            case 'A'://创建多项式Pa
                cin>>j;
                CreatPolyn(Pa, j);
                Out_list(Pa);
                break;
            case 'B'://创建多项式Pb
                cin>>k;
                CreatPolyn(Pb, k);
                Out_list(Pb);
                break;
            case 'S'://多项式相加
                AddPolyn(Pa, Pb);
                Out_list(Pa);
                break;
        }
    }
    return 0;
}
